/* palette-data.vala
 *
 * Copyright (C) 2010  Richard Talbot-Watkins
 * Copyright (C) 2010  Dave Jeffery
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Authors:
 *  Richard Talbot-Watkins <richtw1@gmail.com>
 * 	Dave Jeffery <david.richard.jeffery@gmail.com>
 */

/**
 * Palette Data Model
 *
 * The class is only responsible for storing and manipulating the byte 
 * array used to hold sprite data in the program.
 *
 * @author Richard Talbot-Watkins <richtw1@gmail.com>
 * @author Dave Jeffery <david.richard.jeffery@gmail.com>
 * @version 2010.1221
 * @since 0.1
 */
private class MonsterDesigner.PaletteData : GLib.Object {
	/**
	 * Signal emitted when data changed.
	 */
	public signal void changed ();

	/**
	 * Translation table from BBC Micro colours to Cario colours.
	 */
	private CairoColour[] beeb_colours = {	CairoColour (0, 0, 0),
											CairoColour (1, 0, 0),
											CairoColour (0, 1, 0),
											CairoColour (1, 1, 0),
											CairoColour (0, 0, 1),
											CairoColour (1, 0, 1),        
											CairoColour (0, 1, 1),
											CairoColour (1, 1, 1)};

	/**
	 * Current game palette (with BBC Micro colour indices)
	 */
	private int[] game_palette = {4, 3, 1, 0};

	/**
	 * Sets an entry in the game palette and updates any controls which 
	 * need to display this colour correctly.
	 *
	 * @param logical_colour   the BBC MODE5 logical colour index (0-3)
	 * @param physical_colour  the BBC physical colour (0-7)
	 */
	private void set_palette (int logical_colour, int physical_colour) {
		game_palette[logical_colour] = physical_colour;

		/* Emit changed signal. */			
		changed ();
	}

	public void increment_colour (int logical_colour) {
		var physical_colour = (game_palette[logical_colour] + 1) & 7;
		set_palette (logical_colour, physical_colour);
	}

	public void decrement_colour (int logical_colour) {
		var physical_colour = (game_palette[logical_colour] - 1) & 7;
		set_palette (logical_colour, physical_colour);
	}

	/**
	 * Returns a BBC Micro logical colour as a Cairo RGB value.
	 * 
	 * @param logical_colour A BBC MODE5 logical colour index (0-3).
	 * @return A Cairo RGB value as a CairoColour struct.
	 */
	public CairoColour logical_to_cairo (int logical_colour) {
		return beeb_colours[game_palette[logical_colour]];
	}
}
